package cn.buk.tms.dao;

import cn.buk.tms.entity.misc.Product;
import cn.buk.tms.entity.misc.ProductBought;
import cn.buk.common.util.DateUtil;
import java.util.List;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * User: yfdai
 * Date: 14-4-18
 * Time: 下午12:39
 * To change this template use File | Settings | File Templates.
 */
@Transactional
@Component
public class ProductBoughtDaoImpl extends AbstractDao implements ProductBoughtDao {

    @Override
    public int create(ProductBought productBought) {
        em.persist(productBought);
        em.flush();

        return productBought.getId();
    }

    @Override
    public List<ProductBought> findAll() {
            return em.createQuery("select o from ProductBought o", ProductBought.class)
                    .getResultList();
    }

    @Override
    public int update(ProductBought productBought) {
        em.merge(productBought);
        em.flush();

        return productBought.getId();
    }

    @Override
    public ProductBought getById(int id) {
        return em.find(ProductBought.class, id);
    }

    @Override
    public ProductBought getByProductId(int productId, int enterpriseId) {
        List<ProductBought> productBoughts = em.createQuery("select o from ProductBought o " +
                "where o.product.id = :productId and o.enterpriseId = :enterpriseId", ProductBought.class)
                .setParameter("productId", productId)
                .setParameter("enterpriseId", enterpriseId)
                .getResultList();

        return productBoughts.size() > 0 ? productBoughts.get(0) : null;
    }

    @Override
    public ProductBought getByProductCode(int enterpriseId, String productCode) {
        List<ProductBought> productBoughts = em.createQuery("select o from ProductBought o " +
                "where o.productCode = :code and o.enterpriseId = :enterpriseId", ProductBought.class)
                .setParameter("code", productCode)
                .setParameter("enterpriseId", enterpriseId)
                .getResultList();

        return productBoughts.size() > 0 ? productBoughts.get(0) : null;
    }

    @Override
    public int decProductBoughtCount(int enterpriseId, int id, int count) {
        return em.createQuery("update ProductBought o set o.boughtCount = o.boughtCount - :count where o.id = :id and o.enterpriseId = :enterpriseId")
                .setParameter("id", id)
                .setParameter("enterpriseId", enterpriseId)
                .setParameter("count", count)
                .executeUpdate();
    }

    /**
     * 获取指定产品的购买记录，如果没有，则创建一个
     * @param enterpriseId 企业id
     * @param productCode 产品代码
     * @param boughtCount 默认的购买数量
     * @param expireDays 默认的购买的有效期天数
     * @return
     */
    @Override
    public ProductBought getOrCreateProductBought(final int enterpriseId,final String productCode, final int boughtCount, final int expireDays) {
        Product p = em.createQuery("select o from Product o where o.productCode = :code", Product.class)
                .setParameter("code", productCode)
                .getResultList()
                .get(0);

        List<ProductBought> pbs = em.createQuery("select o from ProductBought o " +
                "where o.enterpriseId = :enterpriseId and o.productCode = :code", ProductBought.class)
                .setParameter("enterpriseId", enterpriseId)
                .setParameter("code", productCode)
                .getResultList();

        if (pbs.size() == 0) {
            ProductBought pb = new ProductBought();
            pb.setEnterpriseId(enterpriseId);
            pb.setProduct(p);
            pb.setProductCode(productCode);
            pb.setBoughtCount(boughtCount);
            pb.setEndTime(DateUtil.addDays(DateUtil.getCurDate(), expireDays));

            em.persist(pb);

            return pb;
        } else {
            return pbs.get(0);
        }
    }
}
